﻿using Kavaa.Core.Data;
using Kavaa.Modules.PortailMedical.Entity;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Kavaa.Modules.PortailMedical.Datalayer
{
    internal static class ReservationDAO
    {

        public static Reservations GetReservationsbloc(int blocId)
        {
            Reservations Reservations = new Reservations();

            foreach (var row in SqlDaoAdapter.ExecuteReader(
                ConfigurationCache.DatabaseConnectionString,
                "[dbo].[sp_PortailMedical_GetReservBloc]"
            , new List<SqlParameter>() { 
                new SqlParameter("@blocId", blocId),
                
               
            }))
            {
                Reservations.Add(new Reservation()
                {
                    Id = (row["ID"] != DBNull.Value) ? Convert.ToInt32(row["ID"]) : 0,
                    DateDemande = (row["DateDemande"] != DBNull.Value) ? Convert.ToDateTime(row["DateDemande"]) : DateTime.Now,
                    DateDebut = (row["DateDebut"] != DBNull.Value) ? Convert.ToDateTime(row["DateDebut"]) : DateTime.Now,
                    DateFin = (row["DateFin"] != DBNull.Value) ? Convert.ToDateTime(row["DateFin"]) : DateTime.Now,
                    etat = (row["etat"] != DBNull.Value) ? Convert.ToInt32(row["etat"]) : 0,

                });
            }

            return Reservations;
        }
        public static void  DeleteReserv(int reservId)
        {

            SqlDaoAdapter.ExecuteNonQuery(ConfigurationCache.DatabaseConnectionString,
               "[dbo].[sp_PortailMedical_DeleteReserv]"
           , new List<SqlParameter>() { 
                new SqlParameter("@ID", reservId),
            });
        }
        public static Boolean existReserv(int id_reserv)
        {
            Reservations Reservations = new Reservations();

            foreach (var row in SqlDaoAdapter.ExecuteReader(
                ConfigurationCache.DatabaseConnectionString,
                "[dbo].[sp_PortailMedical_GetReservById]"
            , new List<SqlParameter>() { 
                new SqlParameter("@ReservId", id_reserv),
                
               
            }))
            {
                Reservations.Add(new Reservation()
                {
                    Id = (row["ID"] != DBNull.Value) ? Convert.ToInt32(row["ID"]) : 0,
                    DateDemande = (row["DateDemande"] != DBNull.Value) ? Convert.ToDateTime(row["DateDemande"]) : DateTime.Now,
                    DateDebut = (row["DateDebut"] != DBNull.Value) ? Convert.ToDateTime(row["DateDebut"]) : DateTime.Now,
                    DateFin = (row["DateFin"] != DBNull.Value) ? Convert.ToDateTime(row["DateFin"]) : DateTime.Now,
                    etat = (row["etat"] != DBNull.Value) ? Convert.ToInt32(row["etat"]) : 0,

                });
            }
            if (Reservations.Count != 0)
            { return true; }
            else { return false; }


        }
        public static int SaveReservation(int id,DateTime DateDebut, DateTime DateFin, int ActeId, int CliniqueId, int MedecinId, String body)
        {
            int ID = 0;
            foreach (var row in

            SqlDaoAdapter.ExecuteReader(ConfigurationCache.DatabaseConnectionString,
                "[dbo].[sp_PortailMedical_SaveReservation]"
           , new List<SqlParameter>() { 
                new SqlParameter("@etat", 0),
                new SqlParameter("@DateDebut", DateDebut),
                 new SqlParameter("@DateFin", DateFin),
                 new SqlParameter("@CliniqueId", CliniqueId),
                 new SqlParameter("@ActeId", ActeId),
                 new SqlParameter("@MedecinId", MedecinId),
                  new SqlParameter("@DateDemande", DateTime.Now),
                   new SqlParameter("@body", body),
                    new SqlParameter("@Id", id)
               
            }))
            { ID = (row["ID"] != DBNull.Value) ? Convert.ToInt32(row["ID"]) : 0; }

            return ID;

        }
        public static void UpdateReservation(DateTime DateDebut, DateTime DateFin, int ActeId, int CliniqueId, int MedecinId, String body,int id)
    {
        SqlDaoAdapter.ExecuteNonQuery(ConfigurationCache.DatabaseConnectionString,
           "[dbo].[sp_PortailMedical_UpdateReservation]"
           , new List<SqlParameter>() { 
                new SqlParameter("@etat", 0),
                new SqlParameter("@DateDebut", DateDebut),
                 new SqlParameter("@DateFin", DateFin),
                 new SqlParameter("@CliniqueId", CliniqueId),
                 new SqlParameter("@ActeId", ActeId),
                 new SqlParameter("@MedecinId", MedecinId),
                  new SqlParameter("@DateDemande", DateTime.Now),
                   new SqlParameter("@body", body),
                   new SqlParameter("@Id", id),
               
            });



    }
        public static Reservations GetReservMedecin(int IdMedecin)
        {
            Reservations Reservations = new Reservations();

            foreach (var row in SqlDaoAdapter.ExecuteReader(
                ConfigurationCache.DatabaseConnectionString,
                "[dbo].[sp_PortailMedical_GetReservMedecin]"
            , new List<SqlParameter>() { 
                new SqlParameter("@IdMedecin",IdMedecin),
                
               
            }))
            {
                Reservations.Add(new Reservation()
                {
                    Id = (row["ID"] != DBNull.Value) ? Convert.ToInt32(row["ID"]) : 0,
                    DateDemande = (row["DateDemande"] != DBNull.Value) ? Convert.ToDateTime(row["DateDemande"]) : DateTime.Now,
                    DateDebut = (row["DateDebut"] != DBNull.Value) ? Convert.ToDateTime(row["DateDebut"]) : DateTime.Now,
                    DateFin = (row["DateFin"] != DBNull.Value) ? Convert.ToDateTime(row["DateFin"]) : DateTime.Now,
                    etat = (row["etat"] != DBNull.Value) ? Convert.ToInt32(row["etat"]) : 0,
                    body = (row["body"] != DBNull.Value) ? Convert.ToString(row["body"]) : null,

                });
            }

            return Reservations;

        }
        public static Reservations GetAllReservations()
        {
            Reservations Reservations = new Reservations();

            foreach (var row in SqlDaoAdapter.ExecuteReader(
                ConfigurationCache.DatabaseConnectionString,
                "[dbo].[sp_PortailMedical_GetAllReservations]"
            , new List<SqlParameter>() { 
                
                
               
            }))
            {
                Reservations.Add(new Reservation()
                {
                    Id = (row["ID"] != DBNull.Value) ? Convert.ToInt32(row["ID"]) : 0,
                    DateDemande = (row["DateDemande"] != DBNull.Value) ? Convert.ToDateTime(row["DateDemande"]) : DateTime.Now,
                    DateDebut = (row["DateDebut"] != DBNull.Value) ? Convert.ToDateTime(row["DateDebut"]) : DateTime.Now,
                    DateFin = (row["DateFin"] != DBNull.Value) ? Convert.ToDateTime(row["DateFin"]) : DateTime.Now,
                    etat = (row["etat"] != DBNull.Value) ? Convert.ToInt32(row["etat"]) : 0,
                    body = (row["body"] != DBNull.Value) ? Convert.ToString(row["body"]) : null,

                });
            }

            return Reservations;

        }
        public static void AddReservCategorie(int ReservId, int BlocId)
        {

            SqlDaoAdapter.ExecuteNonQuery(ConfigurationCache.DatabaseConnectionString,
                "[dbo].[sp_PortailMedical_AddReservCategorie]"
            , new List<SqlParameter>() { 
                new SqlParameter("@ReservId", ReservId),
                new SqlParameter("@BlocId", BlocId),
                
              
               
            });

        }
        public static Reservations GetReservMedecinAll(int IdMedecin)
        {
            Reservations Reservations = new Reservations();

            foreach (var row in SqlDaoAdapter.ExecuteReader(
                ConfigurationCache.DatabaseConnectionString,
                "[dbo].[sp_PortailMedical_GetReservMedecinAll]"
            , new List<SqlParameter>() { 
                new SqlParameter("@IdMedecin",IdMedecin),
                
               
            }))
            {
                Reservations.Add(new Reservation()
                {
                    Id = (row["ID"] != DBNull.Value) ? Convert.ToInt32(row["ID"]) : 0,
                    DateDemande = (row["DateDemande"] != DBNull.Value) ? Convert.ToDateTime(row["DateDemande"]) : DateTime.Now,
                    DateDebut = (row["DateDebut"] != DBNull.Value) ? Convert.ToDateTime(row["DateDebut"]) : DateTime.Now,
                    DateFin = (row["DateFin"] != DBNull.Value) ? Convert.ToDateTime(row["DateFin"]) : DateTime.Now,
                    etat = (row["etat"] != DBNull.Value) ? Convert.ToInt32(row["etat"]) : 0,
                    Acte = new Acte() { Name = (row["ActeName"] != DBNull.Value) ? Convert.ToString(row["ActeName"]) : null },
                    Clinique = new Clinique()
                    {
                        Name = (row["CliniqueName"] != DBNull.Value) ? Convert.ToString(row["CliniqueName"]) : null,
                        Ville = new Ville()
                        {
                            Name = (row["VilleName"] != DBNull.Value) ? Convert.ToString(row["VilleName"]) : null,
                        }
                    },
                    Medecin = new Medecin()
                    {
                        Name = (row["MedecinName"] != DBNull.Value) ? Convert.ToString(row["MedecinName"]) : null,
                        Prenom = (row["MedecinPrenom"] != DBNull.Value) ? Convert.ToString(row["MedecinPrenom"]) : null,
                        INP = (row["INP"] != DBNull.Value) ? Convert.ToString(row["INP"]) : null

                    },

                    body = (row["body"] != DBNull.Value) ? Convert.ToString(row["body"]) : null,

                });
            }

            return Reservations;

        }
        public static Reservations GetReservClinique(int IdClinique)
        {
            Reservations Reservations = new Reservations();

            foreach (var row in SqlDaoAdapter.ExecuteReader(
                ConfigurationCache.DatabaseConnectionString,
                "[dbo].[sp_PortailMedical_GetReservClinique]"
            , new List<SqlParameter>() { 
                new SqlParameter("@IdClinique",IdClinique),
                
               
            }))
            {
                Reservations.Add(new Reservation()
                {
                    Id = (row["ID"] != DBNull.Value) ? Convert.ToInt32(row["ID"]) : 0,
                    DateDemande = (row["DateDemande"] != DBNull.Value) ? Convert.ToDateTime(row["DateDemande"]) : DateTime.Now,
                    DateDebut = (row["DateDebut"] != DBNull.Value) ? Convert.ToDateTime(row["DateDebut"]) : DateTime.Now,
                    DateFin = (row["DateFin"] != DBNull.Value) ? Convert.ToDateTime(row["DateFin"]) : DateTime.Now,
                    etat = (row["etat"] != DBNull.Value) ? Convert.ToInt32(row["etat"]) : 0,
                    Acte = new Acte() { Name = (row["ActeName"] != DBNull.Value) ? Convert.ToString(row["ActeName"]) : null },
                    Clinique = new Clinique() { Name = (row["CliniqueName"] != DBNull.Value) ? Convert.ToString(row["CliniqueName"]) : null },
                    Medecin = new Medecin()
                    {
                        Name = (row["MedecinName"] != DBNull.Value) ? Convert.ToString(row["MedecinName"]) : null,
                        Prenom = (row["MedecinPrenom"] != DBNull.Value) ? Convert.ToString(row["MedecinPrenom"]) : null,
                        INP = (row["INP"] != DBNull.Value) ? Convert.ToString(row["INP"]) : null,
                        Email = (row["MedecinMail"] != DBNull.Value) ? Convert.ToString(row["MedecinMail"]) : null
                    },

                    body = (row["body"] != DBNull.Value) ? Convert.ToString(row["body"]) : null,

                });
            }

            return Reservations;

        }
        public static void ConfirmReserv(int reservId)
        {

            SqlDaoAdapter.ExecuteNonQuery(ConfigurationCache.DatabaseConnectionString,
                "[dbo].[sp_PortailMedical_confirmReserv]"
            , new List<SqlParameter>() { 
                new SqlParameter("@reservId", reservId)
              
               
            });

        }
        public static void RejectReserv(int reservId)
        {

            SqlDaoAdapter.ExecuteNonQuery(ConfigurationCache.DatabaseConnectionString,
                "[dbo].[sp_PortailMedical_rejectReserv]"
            , new List<SqlParameter>() { 
                new SqlParameter("@reservId", reservId)
              
               
            });

        }
        public static String GetBlocReserv(int IdReserv)
        {
            Categories Categories = new Categories();

            foreach (var row in SqlDaoAdapter.ExecuteReader(
                ConfigurationCache.DatabaseConnectionString,
                "[dbo].[sp_PortailMedical_GetBlocReserv]"
            , new List<SqlParameter>() { 
                new SqlParameter("@IdReserv",IdReserv),
                
               
            }))
            {
                Categories.Add(new Categorie()
                {
                    Id = (row["ID"] != DBNull.Value) ? Convert.ToInt32(row["ID"]) : 0,

                    Name = (row["Name"] != DBNull.Value) ? Convert.ToString(row["Name"]) : null,

                });
            }
            if (Categories.Count != 0)
            {
                String blocName = Categories.ElementAt(0).Name;

                return blocName;
            }
            else
            {
                return "";
            }


        }
        public static void UpdateReservConfirmee(int idReserv, int idPatient, string RemarquesPatient)
        {
            SqlDaoAdapter.ExecuteNonQuery(ConfigurationCache.DatabaseConnectionString,
          "[dbo].[sp_PortailMedical_UpdateReservConfirmee]"
          , new List<SqlParameter>() { 
                   new SqlParameter("@RemarquesPatient", RemarquesPatient),
                   new SqlParameter("@IdReserv", idReserv),
                new SqlParameter("@IdPatient", idPatient)
            });

            
        }
        public static Reservations GetReservationPatient(int ReservId)
        {
            Reservations Reservations = new Reservations();

            foreach (var row in SqlDaoAdapter.ExecuteReader(
                ConfigurationCache.DatabaseConnectionString,
                "[dbo].[sp_PortailMedical_GetDossierPatient]"
            , new List<SqlParameter>()
            {
                   new SqlParameter("@ReservId",ReservId),


            }))
            {
                Reservations.Add(new Reservation()
                {
                    Id = (row["ID"] != DBNull.Value) ? Convert.ToInt32(row["ID"]) : 0,
                    Patient = new Patient()
                    {
                        Name = (row["PatientName"] != DBNull.Value) ? Convert.ToString(row["PatientName"]) : null,

                        Prenom = (row["PatientPrenom"] != DBNull.Value) ? Convert.ToString(row["PatientPrenom"]) : null,
                        Age = (row["PatientAge"] != DBNull.Value) ? Convert.ToInt32(row["PatientAge"]) : 0,
                        CIN = (row["PatientCIN"] != DBNull.Value) ? Convert.ToString(row["PatientCIN"]) : null,
                        Mutuelle = new Mutuelle() { Sigle = (row["MutuelleSigle"] != DBNull.Value) ? Convert.ToString(row["MutuelleSigle"]) : null }

                    },
                    Medecin = new Medecin()
                    {
                        Name = (row["MedecinNom"] != DBNull.Value) ? Convert.ToString(row["MedecinNom"]) : null,
                        Prenom = (row["MedecinPrenom"] != DBNull.Value) ? Convert.ToString(row["MedecinPrenom"]) : null

                    },

                    Acte = new Acte() { Name = (row["ActeName"] != DBNull.Value) ? Convert.ToString(row["ActeName"]) : null },
                    DateDebut = (row["DateDebut"] != DBNull.Value) ? Convert.ToDateTime(row["DateDebut"]) : DateTime.Now,
                    DateFin = (row["DateFin"] != DBNull.Value) ? Convert.ToDateTime(row["DateFin"]) : DateTime.Now,


                });
            }


            return Reservations;

        }

    }
}
